<!--
*Author：COCO
 *代码由框架生成,任何更改都可能导致被代码生成器覆盖
 *业务请在@/extension/lreport/bommaster/BomMaster.js此处编写
 -->
<template>
    <view-grid ref="grid"
               :columns="columns"
               :detail="detail"
               :editFormFields="editFormFields"
               :editFormOptions="editFormOptions"
               :searchFormFields="searchFormFields"
               :searchFormOptions="searchFormOptions"
               :table="table"
               :extend="extend">
    </view-grid>
</template>
<script>
    import extend from "@/extension/lreport/bommaster/BomMaster.js";
    import { ref, defineComponent, h, resolveComponent, onMounted } from "vue";
    export default defineComponent({
        setup() {
            const table = ref({
                key: 'BomId',
                footer: "Foots",
                cnName: ' BOM工单主表',
                name: 'bommaster/BomMaster',
                url: "/BomMaster/",
                sortName: "CreateDate"
            });
            const editFormFields = ref({"BomNumber":"","ProductId":"","Version":"","Status":"","Remark":"","IsDefaultBOM":[],"ArtifactType":"","Output":"","BOMReportTitle":"","Attachment":""});
            const editFormOptions = ref([[{"title":"BOM编号","field":"BomNumber","placeholder":"请输入，忽略将自动生成"},
                               {"dataKey":"MBOMRProduct","data":[],"title":"产品ID","field":"ProductId","type":"select"}],
                              [{"title":"BOM版本号","field":"Version"},
                               {"dataKey":"MBOMRState","data":[],"title":"BOM状态","field":"Status","type":"select"}],
                              [{"title":"备注说明","field":"Remark"},
                               {"dataKey":"IsDefaultBOM","data":[],"title":"默认BOM ","field":"IsDefaultBOM","type":"checkbox"}],
                              [{"dataKey":"ProductType","data":[],"title":"成品类型","required":true,"field":"ArtifactType","type":"select"},
                               {"title":"日产量","required":true,"field":"Output","type":"text"}],
                              [{"title":"BOM工单主题","required":true,"field":"BOMReportTitle","type":"text"}],
                              [{"title":"附件","field":"Attachment","type":"file"}]]);
            const searchFormFields = ref({"BomNumber":"","ProductId":""});
            const searchFormOptions = ref([[{"title":"BOM编号","field":"BomNumber","type":"like"},{"dataKey":"MBOMRProduct","data":[],"title":"产品ID","field":"ProductId","type":"select"}]]);
            const columns = ref([{field:'BomId',title:'BOM ID',type:'guid',width:110,hidden:true,readonly:true,require:true,align:'left'},
                       {field:'BomNumber',title:'BOM编号',type:'string',width:110,require:true,align:'left',sort:true},
                       {field:'BOMReportTitle',title:'BOM主题',type:'string',width:110,require:true,align:'left',sort:true},
                       {field:'ProductId',title:'成品ID',type:'guid',bind:{ key:'MBOMRProduct',data:[]},width:110,align:'left'},
                       {field:'ProductCode',title:'成品编号',type:'string',width:110,align:'left'},
                       {field:'ProductName',title:'成品名称',type:'string',width:110,align:'left'},
                       {field:'ProductUPId',title:'上级成品',type:'string',width:110,align:'left'},
                       {field:'Version',title:'BOM版本号',type:'string',width:110,align:'left'},
                       {field:'Status',title:'BOM状态',type:'int',bind:{ key:'MBOMRState',data:[]},width:80,align:'left'},
                       {field:'CreatorId',title:'创建人ID',type:'string',width:110,align:'left'},
                       {field:'CreatorName',title:'创建人姓名',type:'string',width:110,align:'left'},
                       {field:'CreateDate',title:'创建日期',type:'datetime',width:150,align:'left',sort:true},
                       {field:'ModifierId',title:'修改人ID',type:'string',width:110,align:'left'},
                       {field:'ModifierName',title:'修改人姓名',type:'string',width:110,align:'left'},
                       {field:'ModifyDate',title:'修改日期',type:'datetime',width:150,align:'left',sort:true},
                       {field:'Remark',title:'备注说明',type:'string',width:220,align:'left'},
                       {field:'IsDefaultBOM',title:'默认BOM ',type:'int',bind:{ key:'IsDefaultBOM',data:[]},width:80,align:'left'},
                       {field:'ArtifactType',title:'成品类型',type:'int',bind:{ key:'ProductType',data:[]},width:80,require:true,align:'left'},
                       {field:'Output',title:'日产量',type:'int',width:80,require:true,align:'left'},
                       {field:'BOMReportTitle',title:'BOM工单主题',type:'string',width:220,require:true,align:'left'},
                       {field:'Attachment',title:'附件',type:'string',width:220,align:'left'}]);
            const detail = ref({
                cnName: "#detailCnName",
                table: "#detailTable",
                columns: [],
                sortName: "",
                key: ""
            });
            return {
                table,
                extend,
                editFormFields,
                editFormOptions,
                searchFormFields,
                searchFormOptions,
                columns,
                detail,
            };
        },
        mounted() {
            // 在表格渲染后，为操作列的按钮添加点击事件
            setTimeout(() => {
                const actionBtns = document.querySelectorAll('.view-bom-detail-btn');
                if (actionBtns && actionBtns.length > 0) {
                    console.log('找到操作按钮，添加事件处理');
                    actionBtns.forEach(btn => {
                        btn.addEventListener('click', (e) => {
                            e.stopPropagation();
                            // 获取行数据
                            const rowId = btn.getAttribute('data-id');
                            if (rowId && extend && extend.viewBOMDetail) {
                                // 通过ID查找行数据
                                const rowData = this.$refs.grid.getRow(rowId);
                                if (rowData) {
                                    extend.viewBOMDetail.call(extend, rowData);
                                }
                            }
                        });
                    });
                } else {
                    console.error('未找到操作按钮元素');
                }
            }, 1000); // 延迟1秒执行，确保表格已渲染
        }
    });
</script>
